<?php
namespace app\index\controller;
use app\index\controller\Baseali;
use app\service\apiService;
use app\service\invoiceService;
use app\service\orderService;
use think\Controller;
use think\request;
use think\Db;
use think\facade\Log;
error_reporting(0);
class Orderali2 extends Controller{
    public function outpay(){
        $api = new apiService();
        //获取当前用户支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->oauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            return json(['info' => $oauthtoken->sub_msg]);
        }
        $user_id=$oauthtoken->user_id;
        
        $cam_id=request()->get("cam_id");
	    $caminfo=Db::table("parking_camera")->where("id",$cam_id)->find();
	    if(empty($caminfo)){
	        return "相机信息错误";
	    }
	    $this->assign("caminfo",$caminfo);
	    
	    //查询客户端是否在线
	    $clientinfo=Db::table("client_login")->where("clientNo",$caminfo["clientNo"])->where("parkingNo",$caminfo["parkingNo"])->find();
	    if(empty($clientinfo)){
	        return "客户端不在线";
	    }
	    if($clientinfo["connect_status"]=="OFF"){
	        return "客户端不在线";
	    }
	    
	    
	    $client=new \swoole_client(SWOOLE_SOCK_TCP);
        if(!$client->Connect("47.106.184.188",9501,20)){
            echo "errCode:".$client->errCode."\r\n";
            die("connect failed\r\n");
        }
        $msg='{"bizCode":"C1015","parkingNo":"'.$caminfo["parkingNo"].'","clientNo":"'.$caminfo["clientNo"].'","cam_id":"'.$caminfo["channelNo"].'"}';
        $client->send($msg."\r\n");
        //从服务端接收信息
        $data=$client->recv();
        if(!$data){
            return "查询失败,请稍后再试";
            die("recv failed \r\n");
        }
        $client->close();
        $orderinfo=json_decode($data,true);
        if($orderinfo["bizCode"]=="B1006"){
            if($orderinfo["respCode"]=="00000"){
                return $orderinfo["respMsg"];
            }
        }
        
        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$orderinfo["parkingNo"])->find();
        
        $this->assign("parkinginfo",$parkinginfo);
        unset($orderinfo["respMsg"]);
        unset($orderinfo["respCode"]);
        unset($orderinfo["sign"]);
        unset($orderinfo["bizCode"]);
        $orderinfo["parkingName"]=$parkinginfo["parkingName"];
        $orderinfo["user_id"]=$user_id;
        $orderinfo["outChannelNo"]=$caminfo["id"];
        //echo "<pre>";
        //var_dump($orderinfo);die;
        $pay_id=Db::name('car_order_query')->insertGetId($orderinfo);
        $this->assign("pay_id",$pay_id);
        
        $this->assign("orderinfo",$orderinfo);
	    return $this->fetch("orderali/outpay");
    }
    
    public function createOrder(){
		
		$pay_id=request()->post("pay_id");
		$payinfo=Db::table("car_order_query")->where("id",$pay_id)->find();
		if(empty($payinfo)){
		    return json(['info' => "查询账单失败"]);
		}
		
	    
		$out_trade_no=date('YmdHiss').rand(100000,999999);
        if(empty($payinfo["out_trade_no"])){
            
        }
		$token = (new orderService())->gettoken($payinfo["parkingNo"]);
        $subject = $payinfo["parkingName"]."-".$payinfo["carPlateNo"];
        $api = new apiService();
         
        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$payinfo["parkingNo"])->find();    
       
        $notifyUrl="https://www.aliwgzf.com/index/callback/alinotice";
        
        $start_time=date("Y-m-d H:i:s",strtotime($payinfo["parkingStartTime"]));
        $end_time=date("Y-m-d H:i:s");
        
        //获取发票信息
        $invoiceinfo=(new invoiceService())->parkingInvoice($payinfo["parkingNo"]);
        $is_support_invoice="false";
        if($invoiceinfo["is_support_invoice"]=="1"){
            $is_support_invoice="true";
        }
	    $invoice_merchant_name=$invoiceinfo["invoice_merchant_name"];
	    $tax_num=$invoiceinfo["taxrate"];
	    
        $result = $api->orderCreate(json_encode(['out_trade_no'=> $out_trade_no,'total_amount'=>$payinfo["amountRec"]/100,'buyer_id'=>$payinfo["user_id"],'subject' =>$subject,'token'=>$token ,'notifyurl'=>$notifyUrl,"car_number"=>$payinfo["carPlateNo"],"start_time"=>$start_time,"parking_lot_id"=>$parkinginfo["parking_id"],"parkling_lot_name"=>$parkinginfo["parkingName"],"city_code"=>$parkinginfo["city_code"],'is_support_invoice'=>$is_support_invoice,'invoice_merchant_name'=>$invoice_merchant_name,'tax_num'=>$tax_num]));
        //Log::write($result,"yangzhanlei");
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
		    unset($payinfo["id"]);
		    $info=$payinfo;
			$info['tradeno'] =  $result->trade_no;
			$info["out_trade_no"]=$out_trade_no;
			$info["subject"]=$subject;
			
			$res=Db::table("car_order_pay")->insert($info);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $info['tradeno']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
	}
}